<?php

namespace app\nideshop\service;

/**
 * 微信服务类
 */
class Weixin
{

    public static function login($code, $fullUserInfo)
    {
        // array(7) {
        //     ["nickName"] => string(16) "🙄😂[天啊]"
        //     ["gender"] => int(1)
        //     ["language"] => string(5) "zh_CN"
        //     ["city"] => string(8) "Shenzhen"
        //     ["province"] => string(9) "Guangdong"
        //     ["country"] => string(5) "China"
        //     ["avatarUrl"] => string(124) "https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLTohMhFyJGghyvLz7DnI61HZwDPlq6FDz9niaTmWTJYgqpWSGJkBbP9m5XdLFrOY8g88qKRW8hmlw/132"
        // }
        $userInfo = $fullUserInfo['userInfo'];
        $url = 'https://api.weixin.qq.com/sns/jscode2session';
        $grant_type = 'authorization_code';
        $js_code = $code;
        $secret = '36e41533227ddbad53fd4e5a25d7e63b';
        $appid = 'wx116ef319d2614baa';

        $openUrl = $url . '?grant_type=' . $grant_type . '&js_code=' . $js_code . '&secret=' . $secret . '&appid=' . $appid;
        $sessionDataJson = file_get_contents($openUrl);
        $sessionData = json_decode($sessionDataJson, true);
        if (empty($sessionData['openid'])) {
            return [
                'errno' => 500,
                'errmsg' => '调用失败',
                'data' => [
                    'userInfo' => $userInfo
                ]
            ];
        }
        return [
            'errno' => 0,
            'errmsg' => '成功',
            'data' => [
                'userInfo' => $userInfo,
                'sessionData' => $sessionData
            ]
        ];
    }
}
